# encoding: utf-8
class Report::PreRegisterRequestReportController < ApplicationController
  def show
    @title_view = 'Denuncias'
    @complaint_types = RequestAndComplaint::ComplaintType.all
  end

  def show_pdf

    search_pre_register_state = params[search_pre_register_state]
    start_date = params[:calendar][:start_date]
    end_date = params[:calendar][:end_date]

    my_heritage_conditions  = String.new
    my_heritage_values      = Array.new

    my_heritage_conditions << "deleted=?"
    my_heritage_values.push(0)

    if ! start_date.blank?
      my_heritage_conditions << " AND created_at >= ?"
      my_heritage_values.push(start_date.to_date)
    end

    if ! end_date.blank?
      my_heritage_conditions << " AND created_at <= ?"
      my_heritage_values.push(end_date.to_date)
    end

    if ! search_pre_register_state.blank?
      my_heritage_conditions << " AND state=?"
      my_heritage_values.push(search_pre_register_state)
    end

    my_heritage_values.insert(0, my_heritage_conditions)

    @list_prerregisters = CulturalHeritage::PreRegisterRequest.find(:all,:conditions=> my_heritage_values)
    
    pdf = Prawn::Document.new

    pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de información cultural pukara",
      :at => [100, 715],
      :width => 430
    #    Titulo del reporte
    pdf.text_box "Reporte de Solicitudes de prerregistro",
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20
    #    Cuerpo

    pdf.move_down 150

    arr = Array.new

    #Colocando headers
    arr[0] = Array.new
    arr[0][0] = "Id"
    arr[0][1] = "Descripción"
    arr[0][2] = "Usuario"
    arr[0][3] = "Fecha de registro"
    arr[0][4] = "Estado"

    j = 1

    @list_prerregisters.each {
      |prerregister|

      arr[j] = Array.new
      arr[j][0] = prerregister.id.to_s
      arr[j][1] = prerregister.description
      user = User.find(prerregister.user)
      arr[j][2] = user.name << " " << user.lastname
      arr[j][3] = prerregister.created_at.strftime("%d/%m/%Y").to_s
      arr[j][4] = CulturalHeritage::State.find(prerregister.state).name
      j = j + 1
    }

    pdf.table arr, :column_widths => [30, 170, 110, 110, 120], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
      row(0).border_width = 2
      row(0).font_style = :bold
    end

    #Pie de pagina
    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [0, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end
    #Nombre de reporte
    send_data pdf.render, :filename => "Reporte de Denuncias" << ".pdf", :type => "application/pdf"
  end  
end
